package gold.digger;

import javafx.util.Pair;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC446 {
    public long startExecuteTime = System.currentTimeMillis();


    public class Solution {

        public int numberOfArithmeticSlices(int[] nums) {
            int len = nums.length;
            if (len < 3) {
                return 0;
            }

            // dp[i]：以 nums[i] 结尾，公差为 key 的长度大于等于 2 的等差数列的个数
            Map<Long, Integer>[] dp = new HashMap[len];
            for (int i = 0; i < len; i++) {
                dp[i] = new HashMap<>();
            }

            int res = 0;
            for (int i = 1; i < len; i++) {
                for (int j = 0; j < i; j++) {
                    long diff = (long) nums[i] - nums[j];
                    if (diff > Integer.MAX_VALUE || diff < Integer.MIN_VALUE) {
                        continue;
                    }

                    // 与之前的等差数列公差相等的时候，说明可以接上，此时计算结果
                    dp[i].put(diff, dp[i].getOrDefault(diff, 0) + dp[j].getOrDefault(diff, 0) + 1);
                    if (dp[j].containsKey(diff)) {
                        res += dp[j].get(diff);
                    }
                }
            }
            return res;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC446 an = new LC446();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
